﻿using Core;
using Core.Data;
using Core.Domain.Common;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Services.ExtendedAttributes
{
    /// <summary>
    /// 扩展属性服务实现类
    /// </summary>
    public partial class ExtendedAttributeService : IExtendedAttributeService
    {
        #region Fields

        private readonly IRepository<ExtendedAttribute> _extendedRepository;
        private readonly IRepository<ExtendedAttributeValue> _valueRepository;
        #endregion
        
        #region Ctor

        public ExtendedAttributeService(
            IRepository<ExtendedAttribute> extendedRepository,
            IRepository<ExtendedAttributeValue> valueRepository)
        {
            this._extendedRepository = extendedRepository;
            this._valueRepository = valueRepository;
        }

        #endregion

        #region ExtendedAttribute
        public virtual IPagedList<ExtendedAttribute> GetAllExtendedAttributes(string name, int pageIndex, int pageSize)
        {
            var query = _extendedRepository.Table;

            if (!String.IsNullOrWhiteSpace(name))
                query = query.Where(c => name.Contains(c.Name));

            query = query.OrderBy(c => c.DisplayOrder);

            return new PagedList<ExtendedAttribute>(query, pageIndex, pageSize);
        }

        public virtual void DeleteExtendedAttribute(ExtendedAttribute entity)
        {
            if (entity == null)
                throw new ArgumentNullException("ExtendedAttribute");

            _extendedRepository.Delete(entity);
        }

        public virtual ExtendedAttribute GetExtendedAttributeById(int extendedAttributeId)
        {
            if (extendedAttributeId == 0)
                return null;
            return _extendedRepository.GetById(extendedAttributeId);
        }

        /// <summary>
        /// 获取集合
        /// </summary>
        /// <param name="ExtendedIds"></param>
        /// <returns></returns>
        public virtual IList<ExtendedAttribute> GetExtendedAttributeByIds(int[] ExtendedIds)
        {
            var query = _extendedRepository.Table;

            if (ExtendedIds != null && ExtendedIds.Length > 0)
            {
                query = query.Where(c => ExtendedIds.Contains(c.Id));
                return query.ToList();
            }
            else
            {
                return new List<ExtendedAttribute>();
            }
        }

        /// <summary>
        /// 根据枚举获取所有属性
        /// </summary>
        /// <param name="extend"></param>
        /// <returns></returns>
        public IList<ExtendedAttribute> GetExtendedAttributeByEnum(Core.Domain.ExtendEnum extend)
        {
            if (extend == null)
                return new List<ExtendedAttribute>();

            var query = _extendedRepository.Table;
            return query.Where(x => x.ExtendEnumId == (int)extend).ToList();
        }

        public virtual void InsertExtendedAttribute(ExtendedAttribute entity)
        {
            if (entity == null)
                throw new ArgumentNullException("ExtendedAttribute");
            _extendedRepository.Insert(entity);

        }

        public virtual void UpdateExtendedAttribute(ExtendedAttribute entity)
        {
            if (entity == null)
                throw new ArgumentNullException("ExtendedAttribute");

            _extendedRepository.Update(entity);
        }
        #endregion

        #region Values
        public IList<ExtendedAttributeValue> GetAllExtendValues(int ExtendedId)
        {
            var list = (from c in _valueRepository.Table
                         orderby c.DisplayOrder
                         where c.ExtendedAttributeId == ExtendedId
                         select c).ToList();
            return list;
        }

        public void InsertValue(ExtendedAttributeValue entity)
        {
            if(entity ==null)
                throw new ArgumentNullException("ExtendedAttributeValue");

            _valueRepository.Insert(entity);
        }

        public void UpdateValue(ExtendedAttributeValue entity)
        {
            if (entity == null)
                throw new ArgumentNullException("ExtendedAttributeValue");

            _valueRepository.Update(entity);
        }

        public ExtendedAttributeValue GetValueById(int valueId)
        {
            if (valueId == 0)
                return null;
            return _valueRepository.GetById(valueId);
        }

        public void DeleteValue(ExtendedAttributeValue entity)
        {
            if (entity == null)
                throw new ArgumentNullException("ExtendedAttributeValue");

            _valueRepository.Delete(entity);
        }

        public void DeleteValues(IList<ExtendedAttributeValue> entitys)
        {
            if (entitys.Count > 0)
            {
                foreach (ExtendedAttributeValue entity in entitys)
                {
                    DeleteValue(entity);
                }
            }
        }
        #endregion


    }
}
